#!/usr/bin/python

import sys

f=open(sys.argv[1], 'r')
team=[False for i in range(11)]
distSelf=100.0
distOpponent=0.0
distSelfOpponent=0.0
MIN_DIST=1.0
possession=0
possessionLoop=-1
currentTime=0
MIN_DELTA_PASS=int(sys.argv[2]) #ball must travel a bit
MAX_DELTA_PASS=int(sys.argv[3]) #nobody got it, it's not a pass anymore
passTotal=0
passTime=-MIN_DELTA_PASS-1
passCorrect=0
passCorrectTime=-MIN_DELTA_PASS-1
for l in f.readlines():
  if l.find('pass')!=-1:
    if l[:3]=='dgk':
      currentTime=int(l[18:l.find(':')])
    else:
      currentTime=int(l[19:l.find(':')])
    if currentTime-passTime>=MIN_DELTA_PASS:
      passTotal+=1
    passTime=currentTime
  elif l.find('possession')!=-1:
    v=l.strip().split(' ')
    unum=int(v[3])-1
    currentTime=int(v[2][1:v[2].find(':')])
    if team[unum]:
      #every player in the team was analyzed
      if distSelf<distOpponent and distSelfOpponent>=MIN_DIST:
        #print 'possession dgk'
        if possessionLoop==-1:
          possessionLoop=currentTime #starting
        if currentTime-passTime>=MIN_DELTA_PASS and currentTime-passTime<=MAX_DELTA_PASS:
          if currentTime-passCorrectTime>=MIN_DELTA_PASS:
            passCorrect+=1
            passCorrectTime=currentTime
      else:
        #print 'possession web'
        if possessionLoop!=-1:
          possession+=currentTime-possessionLoop #ball changed teams
          possessionLoop=-1
      distSelf=100.0
      team=[False for i in range(11)]
    distSelfLoop=float(v[4])
    distOpponentLoop=float(v[5])
    distSelfOpponentLoop=float(v[6])
    if distSelfLoop<distSelf:
      distSelf=distSelfLoop
      distOpponent=distOpponentLoop
      distSelfOpponent=distSelfOpponentLoop
    team[unum]=True
f.close()
if possessionLoop!=-1:
  possession+=currentTime-possessionLoop
print 'Ball possession: %.0f%%' % (100*float(possession)/float(currentTime))
print passCorrect, 'correct passes out of', passTotal, '(%.0f%%)' % (100*float(passCorrect)/float(passTotal))
